
/*
This file is part of MJIN2.

Copyright (C) 2012-2013 Opensource Game Studio

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#include <mjin2/Common.h>
#include <mjin2/SoundExceptions_p.h>
#include <mjin2/SoundSourceMultiBuffer.h>

namespace mjin2
{

SoundSourceMultiBuffer::SoundSourceMultiBuffer(u8 buffersNb) throw () :
    mBuffersNb(buffersNb),
    mVolume(1),
    mEnabled(true) { }

SoundSourceMultiBuffer::~SoundSourceMultiBuffer() throw ()
{
    removeAll();
}

SOUND_SOURCE_STATE SoundSourceMultiBuffer::state() const throw ()
{
    for (auto it = mPairs.begin(); it != mPairs.end(); ++it)
        if ((*it)->source->state() != SOUND_SOURCE_STATE_STOPPED)
            return (*it)->source->state();
    return SOUND_SOURCE_STATE_STOPPED;
}

float SoundSourceMultiBuffer::volume() const throw ()
{
    return mVolume;
}

bool SoundSourceMultiBuffer::isEnabled() const throw ()
{
    return mEnabled;
}

void SoundSourceMultiBuffer::pushBuffer(SoundBuffer *buf, bool skipSame) throw (Exception)
{
    if (!mEnabled)
        return;
    removeStoppedPairs();
    if (skipSame)
        for (auto it = mPairs.begin(); it != mPairs.end(); ++it)
            if ((*it)->buffer == buf)
                return;
    Pair *pair = new Pair(buf);
    pair->source->setVolume(mVolume);
    mPairs.push_back(pair);
    while (mPairs.size() > mBuffersNb)
        removeFirstPair();
}

void SoundSourceMultiBuffer::setEnabled(bool enabled) throw (Exception)
{
    if (mEnabled == enabled)
        return;
    if (state() != SOUND_SOURCE_STATE_STOPPED)
        setState(SOUND_SOURCE_STATE_STOPPED);
    mEnabled = enabled;
}

void SoundSourceMultiBuffer::setState(SOUND_SOURCE_STATE state) throw (Exception)
{
    if (!mEnabled)
        return;
    switch (state)
    {
        case SOUND_SOURCE_STATE_STOPPED:
            removeAll();
            break;
        case SOUND_SOURCE_STATE_PLAYING:
        case SOUND_SOURCE_STATE_PAUSED:
            for (auto it = mPairs.begin(); it != mPairs.end(); ++it)
                (*it)->source->setState(state);
            break;
        default:
            MJIN2_SOUND_EXCEPTION(
                MJIN2_STR("Unsupported sound source state: %d", state));
    }
}

void SoundSourceMultiBuffer::setVolume(float value) throw (Exception)
{
    mVolume = value;
    for (auto it = mPairs.begin(); it != mPairs.end(); ++it)
        (*it)->source->setVolume(value);
}

void SoundSourceMultiBuffer::removeAll() throw ()
{
    while (!mPairs.empty())
        removeFirstPair();
}

void SoundSourceMultiBuffer::removeFirstPair() throw ()
{
    auto it = mPairs.begin();
    delete *it;
    mPairs.erase(it);
}

void SoundSourceMultiBuffer::removeStoppedPairs() throw ()
{
    auto it = mPairs.begin();
    while (it != mPairs.end())
    {
        if ((*it)->source->state() == SOUND_SOURCE_STATE_STOPPED)
        {
            delete *it;
            mPairs.erase(it++);
        }
        else
            ++it;
    }
}

SoundSourceMultiBuffer::Pair::Pair(SoundBuffer *buf) throw (Exception) : buffer(buf)
{
    source = new SoundSourceBuffer();
    source->setBuffer(buf);
}

SoundSourceMultiBuffer::Pair::~Pair() throw (Exception)
{
    source->setState(SOUND_SOURCE_STATE_STOPPED);
    source->setBuffer(0);
    delete source;
}

} // namespace mjin2

